{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2.14 \u5408\u5e76\u62fc\u63a5\u5b57\u7b26\u4e32\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u51e0\u4e2a\u5c0f\u7684\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\u5927\u7684\u5b57\u7b26\u4e32"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u8981\u5408\u5e76\u7684\u5b57\u7b26\u4e32\u662f\u5728\u4e00\u4e2a\u5e8f\u5217\u6216\u8005 iterable \u4e2d\uff0c\u90a3\u4e48\u6700\u5feb\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528 join() \u65b9\u6cd5\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parts = ['Is', 'Chicago', 'Not', 'Chicago?']\n' '.join(parts)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "','.join(parts)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "''.join(parts)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u770b\u8d77\u6765\uff0c\u8fd9\u79cd\u8bed\u6cd5\u770b\u4e0a\u53bb\u4f1a\u6bd4\u8f83\u602a\uff0c\u4f46\u662f join() \u88ab\u6307\u5b9a\u4e3a\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002\n\u8fd9\u6837\u505a\u7684\u90e8\u5206\u539f\u56e0\u662f\u4f60\u60f3\u53bb\u8fde\u63a5\u7684\u5bf9\u8c61\u53ef\u80fd\u6765\u81ea\u5404\u79cd\u4e0d\u540c\u7684\u6570\u636e\u5e8f\u5217(\u6bd4\u5982\u5217\u8868\uff0c\u5143\u7ec4\uff0c\u5b57\u5178\uff0c\u6587\u4ef6\uff0c\u96c6\u5408\u6216\u751f\u6210\u5668\u7b49)\uff0c\n\u5982\u679c\u5728\u6240\u6709\u8fd9\u4e9b\u5bf9\u8c61\u4e0a\u90fd\u5b9a\u4e49\u4e00\u4e2a join() \u65b9\u6cd5\u660e\u663e\u662f\u5197\u4f59\u7684\u3002\n\u56e0\u6b64\u4f60\u53ea\u9700\u8981\u6307\u5b9a\u4f60\u60f3\u8981\u7684\u5206\u5272\u5b57\u7b26\u4e32\u5e76\u8c03\u7528\u4ed6\u7684 join() \u65b9\u6cd5\u53bb\u5c06\u6587\u672c\u7247\u6bb5\u7ec4\u5408\u8d77\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4ec5\u4ec5\u53ea\u662f\u5408\u5e76\u5c11\u6570\u51e0\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u52a0\u53f7(+)\u901a\u5e38\u5df2\u7ecf\u8db3\u591f\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 'Is Chicago'\nb = 'Not Chicago?'\na + ' ' + b"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u52a0\u53f7(+)\u64cd\u4f5c\u7b26\u5728\u4f5c\u4e3a\u4e00\u4e9b\u590d\u6742\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u66ff\u4ee3\u65b9\u6848\u7684\u65f6\u5019\u901a\u5e38\u4e5f\u5de5\u4f5c\u7684\u5f88\u597d\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('{} {}'.format(a,b))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(a + ' ' + b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5728\u6e90\u7801\u4e2d\u5c06\u4e24\u4e2a\u5b57\u9762\u5b57\u7b26\u4e32\u5408\u5e76\u8d77\u6765\uff0c\u4f60\u53ea\u9700\u8981\u7b80\u5355\u7684\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u8d77\uff0c\u4e0d\u9700\u8981\u7528\u52a0\u53f7(+)\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 'Hello' 'World'\na"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b57\u7b26\u4e32\u5408\u5e76\u53ef\u80fd\u770b\u4e0a\u53bb\u5e76\u4e0d\u9700\u8981\u7528\u4e00\u6574\u8282\u6765\u8ba8\u8bba\u3002\n\u4f46\u662f\u4e0d\u5e94\u8be5\u5c0f\u770b\u8fd9\u4e2a\u95ee\u9898\uff0c\u7a0b\u5e8f\u5458\u901a\u5e38\u5728\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65f6\u5019\u56e0\u4e3a\u9009\u62e9\u4e0d\u5f53\u800c\u7ed9\u5e94\u7528\u7a0b\u5e8f\u5e26\u6765\u4e25\u91cd\u6027\u80fd\u635f\u5931\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u91cd\u8981\u7684\u9700\u8981\u5f15\u8d77\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u6211\u4eec\u4f7f\u7528\u52a0\u53f7(+)\u64cd\u4f5c\u7b26\u53bb\u8fde\u63a5\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u662f\u975e\u5e38\u4f4e\u6548\u7387\u7684\uff0c\n\u56e0\u4e3a\u52a0\u53f7\u8fde\u63a5\u4f1a\u5f15\u8d77\u5185\u5b58\u590d\u5236\u4ee5\u53ca\u5783\u573e\u56de\u6536\u64cd\u4f5c\u3002\n\u7279\u522b\u7684\uff0c\u4f60\u6c38\u8fdc\u90fd\u4e0d\u5e94\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u5b57\u7b26\u4e32\u8fde\u63a5\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = ''\nfor p in parts:\n    s += p"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u5199\u6cd5\u4f1a\u6bd4\u4f7f\u7528 join() \u65b9\u6cd5\u8fd0\u884c\u7684\u8981\u6162\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u6267\u884c+=\u64cd\u4f5c\u7684\u65f6\u5019\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\n\u4f60\u6700\u597d\u662f\u5148\u6536\u96c6\u6240\u6709\u7684\u5b57\u7b26\u4e32\u7247\u6bb5\u7136\u540e\u518d\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u76f8\u5bf9\u6bd4\u8f83\u806a\u660e\u7684\u6280\u5de7\u662f\u5229\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f(\u53c2\u80031.19\u5c0f\u8282)\u8f6c\u6362\u6570\u636e\u4e3a\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u5408\u5e76\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = ['ACME', 50, 91.1]\n','.join(str(d) for d in data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u6837\u8fd8\u5f97\u6ce8\u610f\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u3002\u6709\u65f6\u5019\u7a0b\u5e8f\u5458\u5728\u6ca1\u6709\u5fc5\u8981\u505a\u8fde\u63a5\u64cd\u4f5c\u7684\u65f6\u5019\u4ecd\u7136\u591a\u6b64\u4e00\u4e3e\u3002\u6bd4\u5982\u5728\u6253\u5370\u7684\u65f6\u5019\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(a + ':' + b + ':' + c) # Ugly\nprint(':'.join([a, b, c])) # Still ugly\nprint(a, b, c, sep=':') # Better"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6df7\u5408\u4f7f\u7528I/O\u64cd\u4f5c\u548c\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u6709\u65f6\u5019\u9700\u8981\u4ed4\u7ec6\u7814\u7a76\u4f60\u7684\u7a0b\u5e8f\u3002\n\u6bd4\u5982\uff0c\u8003\u8651\u4e0b\u9762\u7684\u4e24\u7aef\u4ee3\u7801\u7247\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Version 1 (string concatenation)\nf.write(chunk1 + chunk2)\n\n# Version 2 (separate I/O operations)\nf.write(chunk1)\nf.write(chunk2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5f88\u5c0f\uff0c\u90a3\u4e48\u7b2c\u4e00\u4e2a\u7248\u672c\u6027\u80fd\u4f1a\u66f4\u597d\u4e9b\uff0c\u56e0\u4e3aI/O\u7cfb\u7edf\u8c03\u7528\u5929\u751f\u5c31\u6162\u3002\n\u53e6\u5916\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5f88\u5927\uff0c\u90a3\u4e48\u7b2c\u4e8c\u4e2a\u7248\u672c\u53ef\u80fd\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\n\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u521b\u5efa\u4e00\u4e2a\u5f88\u5927\u7684\u4e34\u65f6\u7ed3\u679c\u5e76\u4e14\u8981\u590d\u5236\u5927\u91cf\u7684\u5185\u5b58\u5757\u6570\u636e\u3002\n\u8fd8\u662f\u90a3\u53e5\u8bdd\uff0c\u6709\u65f6\u5019\u662f\u9700\u8981\u6839\u636e\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7279\u70b9\u6765\u51b3\u5b9a\u5e94\u8be5\u4f7f\u7528\u54ea\u79cd\u65b9\u6848\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8c08\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u51c6\u5907\u7f16\u5199\u6784\u5efa\u5927\u91cf\u5c0f\u5b57\u7b26\u4e32\u7684\u8f93\u51fa\u4ee3\u7801\uff0c\n\u4f60\u6700\u597d\u8003\u8651\u4e0b\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\uff0c\u5229\u7528yield\u8bed\u53e5\u4ea7\u751f\u8f93\u51fa\u7247\u6bb5\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def sample():\n    yield 'Is'\n    yield 'Chicago'\n    yield 'Not'\n    yield 'Chicago?'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6cd5\u4e00\u4e2a\u6709\u8da3\u7684\u65b9\u9762\u662f\u5b83\u5e76\u6ca1\u6709\u5bf9\u8f93\u51fa\u7247\u6bb5\u5230\u5e95\u8981\u600e\u6837\u7ec4\u7ec7\u505a\u51fa\u5047\u8bbe\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u7684\u4f7f\u7528 join() \u65b9\u6cd5\u5c06\u8fd9\u4e9b\u7247\u6bb5\u5408\u5e76\u8d77\u6765\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "text = ''.join(sample())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6216\u8005\u4f60\u4e5f\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7247\u6bb5\u91cd\u5b9a\u5411\u5230I/O\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for part in sample():\n    f.write(part)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u518d\u6216\u8005\u4f60\u8fd8\u53ef\u4ee5\u5199\u51fa\u4e00\u4e9b\u7ed3\u5408I/O\u64cd\u4f5c\u7684\u6df7\u5408\u65b9\u6848\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def combine(source, maxsize):\n    parts = []\n    size = 0\n    for part in source:\n        parts.append(part)\n        size += len(part)\n        if size > maxsize:\n            yield ''.join(parts)\n            parts = []\n            size = 0\n    yield ''.join(parts)\n\n# \u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\nwith open('filename', 'w') as f:\n    for part in combine(sample(), 32768):\n        f.write(part)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u5728\u4e8e\u539f\u59cb\u7684\u751f\u6210\u5668\u51fd\u6570\u5e76\u4e0d\u9700\u8981\u77e5\u9053\u4f7f\u7528\u7ec6\u8282\uff0c\u5b83\u53ea\u8d1f\u8d23\u751f\u6210\u5b57\u7b26\u4e32\u7247\u6bb5\u5c31\u884c\u4e86\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}